package xjtu.se.articlecommunity.user.articledetail;

import org.springframework.web.bind.annotation.*;
import xjtu.se.articlecommunity.common.CommonService;
import xjtu.se.articlecommunity.common.Result;
import xjtu.se.articlecommunity.common.TokenUtils;
import xjtu.se.articlecommunity.common.dto.CurrentUser;
import xjtu.se.articlecommunity.user.articledetail.dto.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/user/articledetail")
public class ArticleDetailAPI {
    @Resource
    private ArticleDetailService articleDetailService;

    @Resource
    private CommonService commonService;
    @PostMapping("/likeArticle")
    public Result likeArticle(@RequestBody  LikeArticleDto likeArticleDto){
        int s=articleDetailService.userLiking(likeArticleDto.getU_id(),likeArticleDto.getA_id());
        return Result.success(s);
    }
    @PostMapping("/likeComment")
    public Result likeComment(@RequestBody LikeCommentDto commentDto){
        System.out.println(commentDto.getU_id()+","+commentDto.getC_id());
        int s=articleDetailService.likeComment(commentDto);
        return Result.success(s);
    }


    @GetMapping("/info")
    public Result getArticleInfo(String u_name){
        Article article=articleDetailService.getArticleInfo(u_name);
        return Result.success(article);
    }
    @PostMapping("/comment")
    public Result getArticleInfo(@RequestBody CommentDto commentDto){
        articleDetailService.comment(commentDto);
        return Result.success();
    }
    @PostMapping("/reply")
    public Result reply(@RequestBody ReplyDto replyDto){
        articleDetailService.reply(replyDto.getReply_detail(),replyDto.getU_id(),replyDto.getC_id());
        return Result.success();
    }


    @GetMapping("/wholiking/{a_id}")
    public Result likingUsers(@PathVariable String a_id){
        List<User> userList=articleDetailService.getLikingUsers(a_id);
        return Result.success(userList);
    }

    @GetMapping("/whocomment/{a_id}")
    public Result comments(@RequestHeader("Token") String token, @PathVariable String a_id){
        CurrentUser currentUser = TokenUtils.getUserInfo(token,commonService);
        ArrayList<CommentInfo> list= articleDetailService.getCommentInfo(currentUser.getU_id(),a_id);
        return Result.success(list);
    }


    @GetMapping("/search/{value}")
    public Result search(@PathVariable String value){
        List<SearchInfo> list=articleDetailService.getSearchInfo(value);
        return Result.success(list);
    }

    @GetMapping("/info/{a_id}")
    public Result getInfo(@PathVariable String a_id){
        Article article=articleDetailService.getArticle(a_id);
        return Result.success(article);
    }

    @GetMapping("/tab/{a_id}")
    public Result getUserTab(@PathVariable String a_id){

        return Result.success(articleDetailService.getUserTab(a_id));
    }
    @GetMapping("/articleTabs/{a_id}")
    public Result getArticleTabs(@PathVariable String a_id){
        String[] tabs=articleDetailService.getArticleTabs(a_id);
        //Map<String,Object> map=new HashMap<String, Object>();
        //for(int i=1;i<=tabs.length;i++){
            //map.put("tab",tabs[i-1]);
        //}
        return Result.success(tabs);
    }

    //关注
    @PostMapping("/attend")
    public Result attend(@RequestBody LikeArticleDto dto){
        int ifAttend=articleDetailService.attend(dto);

        return Result.success(ifAttend);
    }
    //收藏
    @PostMapping("/collect")
    public Result collect(@RequestBody LikeArticleDto dto){
        int ifCollect=articleDetailService.collect(dto);
        return Result.success(ifCollect);
    }
    //获取收藏数
    @GetMapping("/collectNum/{a_id}")
    public Result collectNum(@PathVariable String a_id){
        int collectNum=articleDetailService.getCollectNum(a_id);
        return Result.success(collectNum);
    }

    @GetMapping("/ifLikeArticle/{a_id}")
    public Result ifLikeArticle(@RequestHeader("Token") String token,@PathVariable String a_id){

        CurrentUser currentUser = TokenUtils.getUserInfo(token,commonService);
        int ifLikeArticle=articleDetailService.getIfLikeArticle(currentUser.getU_id(),a_id);
        return Result.success(ifLikeArticle);
    }
    @GetMapping("/ifCollectArticle/{a_id}")
    public Result ifCollectArticle(@RequestHeader("Token") String token,@PathVariable String a_id){
        CurrentUser currentUser = TokenUtils.getUserInfo(token,commonService);
        int ifCollectArticle=articleDetailService.getIfCollectArticle(currentUser.getU_id(),a_id);
        return Result.success(ifCollectArticle);
    }

    @GetMapping("/ifAttendUser/{a_id}")
    public Result ifAttendUser(@RequestHeader("Token") String token,@PathVariable String a_id){
        CurrentUser currentUser = TokenUtils.getUserInfo(token,commonService);
        int ifAttendUser=articleDetailService.getIfAttendUser(currentUser.getU_id(),a_id);
        return Result.success(ifAttendUser);
    }





    
}
